package com.librelato.tela;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

import com.librelato.bean.BemAtivoBean;
import com.librelato.bean.SolicitacaoManutencaoBean;
import com.librelato.bean.TipoServicoManutencaoBean;
import com.librelato.bean.UsuarioBean;
import com.librelato.negocio.SolicitacaoManutencaoServico;
import com.librelato.negocio.interfaces.IServicoGenerico;
import com.librelato.webservices.DadosManu;
import com.librelato.webservices.WsSoapStubManutencao;

@ManagedBean
@ViewScoped
public class SolicitacaoManutencaoTela extends TelaAbstrata {

	@ManagedProperty(value = "#{usuario}")
	private UsuarioBean usuario;

	private SolicitacaoManutencaoBean solicitaManu;

	private SolicitacaoManutencaoServico<SolicitacaoManutencaoBean> solicitaManutencaoServico;

	private Map<String, String> param;

	private String numeroSolicitacaoServico;

	private BemAtivoBean bemSelecionado;

	public SolicitacaoManutencaoTela() {
		this.solicitaManutencaoServico = new SolicitacaoManutencaoServico<SolicitacaoManutencaoBean>();
		this.param = new HashMap<String, String>();
		solicitaManu = new SolicitacaoManutencaoBean();

	}

	@SuppressWarnings("rawtypes")
	@Override
	protected IServicoGenerico getServico() {
		// TODO Auto-generated method stub
		return this.solicitaManutencaoServico;
	}

	public List<BemAtivoBean> autoCompleteBemAtivo(String query) {
		List<BemAtivoBean> bens = new ArrayList<BemAtivoBean>();
		try {
			param.put("CODBEM", query);
			param.put("FILIAL", solicitaManu.getFilial());
			bens = solicitaManutencaoServico.buscaBemAtivos(param);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bens;
	}

	public List<TipoServicoManutencaoBean> autoCompleteTipoServico(String query) {
		List<TipoServicoManutencaoBean> tiposServico = new ArrayList<TipoServicoManutencaoBean>();
		try {
			param.put("CODTIPO", query);
			tiposServico = solicitaManutencaoServico.buscaTiposServico(param);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return tiposServico;
	}

	public void salvar() {
		FacesMessage msg = null;
		try {

			if (usuario.getCodMicrosiga() != null
					&& !"".equals(usuario.getCodMicrosiga())) {

				DadosManu oSolici = new DadosManu();
				oSolici.setCodBem(solicitaManu.getBemAtivo().getCodigo());
				oSolici.setCodSolici(usuario.getCodMicrosiga());
				oSolici.setDescServ(solicitaManu.getDescricaoServico());
				oSolici.setFilial(solicitaManu.getFilial());
				oSolici.setNomeSolici(usuario.getNome());
				oSolici.setRamal(solicitaManu.getRamal());
				oSolici.setTipoManut(solicitaManu.getTipoManutencao());
				oSolici.setTipoServ(solicitaManu.getTipoServico().getCodigo());
				WsSoapStubManutencao webService = new WsSoapStubManutencao();
				numeroSolicitacaoServico = webService.inclui(oSolici);

				msg = new FacesMessage(FacesMessage.SEVERITY_INFO, this
						.getServico().getValor("msg_header_sucesso"), this
						.getServico().getValor("msg_solicitacao_incluir"));

				RequestContext.getCurrentInstance().execute(
						"dlgSucessoSolServ.show()");
				solicitaManu = new SolicitacaoManutencaoBean();
			} else {
				msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, this
						.getServico().getValor("msg_header_erro"), this
						.getServico().getValor("msg_sem_cod_usuario_cd_siga"));
			}

		} catch (Exception e) {
			msg = new FacesMessage(FacesMessage.SEVERITY_FATAL, this
					.getServico().getValor("msg_header_erro"), this
					.getServico().getValor("msg_valida_inclusao_solicita"));
			e.printStackTrace();
		}
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void onSelectBem(SelectEvent event) {
		solicitaManu.setBemAtivo((BemAtivoBean) event.getObject());
		bemSelecionado = null;
	}

	public SolicitacaoManutencaoBean getSolicitaManu() {
		return solicitaManu;
	}

	public void setSolicitaManu(SolicitacaoManutencaoBean solicitaManu) {
		this.solicitaManu = solicitaManu;
	}

	public Map<String, String> getParam() {
		return param;
	}

	public void setParam(Map<String, String> param) {
		this.param = param;
	}

	public UsuarioBean getUsuario() {
		return usuario;
	}

	public void setUsuario(UsuarioBean usuario) {
		this.usuario = usuario;
	}

	public String getNumeroSolicitacaoServico() {
		return numeroSolicitacaoServico;
	}

	public void setNumeroSolicitacaoServico(String numeroSolicitacaoServico) {
		this.numeroSolicitacaoServico = numeroSolicitacaoServico;
	}

	public BemAtivoBean getBemSelecionado() {
		return bemSelecionado;
	}

	public void setBemSelecionado(BemAtivoBean bemSelecionado) {
		this.bemSelecionado = bemSelecionado;
	}

}
